home *** CD-ROM | disk | FTP | other *** search
/ The PC-SIG Library 10 / The PC-Sig Library - Shareware for the IBM PC and Compatibles (PC-SIG)(Tenth Edition Disks 1-2804)(1991).iso / PC_SIGCD / 22 / 4 / DISK2247.ZIP / CBASE101.ZIP / CBASE.ZIP / CBEXP.C < prev    next >
Text File  |  1990-06-21  |  9KB  |  459 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)cbexp.c    1.4 - 90/06/20" */
  5.  
  6. /* ansi headers */
  7. #include <ctype.h>
  8. #include <errno.h>
  9. #include <limits.h>
  10. #include <stdio.h>
  11. /*#include <stddef.h>*/
  12. /*#include <string.h>*/
  13.  
  14. /* local headers */
  15. #include "cbase_.h"
  16.  
  17. /*man---------------------------------------------------------------------------
  18. NAME
  19.      cbexp - cbase export functions
  20.  
  21. SYNOPSIS
  22.  
  23. DESCRIPTION
  24.  
  25. SEE ALSO
  26.      cbcmp, cbimp.
  27.  
  28. DIAGNOSTICS
  29.  
  30. WARNINGS
  31.      Pointer format is implementation defined; it could possibly
  32.      contain the field delimiter ('|') or escape ('\\\\') chars.  If
  33.      so, the field delimiter or escape character will have to be
  34.      changed.
  35.  
  36. ------------------------------------------------------------------------------*/
  37. /* array data type export macro */
  38. #define vexp(EXPFCT) {                            \
  39.     int i = 0;                            \
  40.     int nelems = n / sizeof(*cp);                    \
  41.                                     \
  42.     for (i = 0; i < nelems; ++i) {                    \
  43.         if (EXPFCT(fp, cp, sizeof(*cp)) == -1) return -1;    \
  44.         if (fputc(EXPFLDDLM, fp) == EOF) return -1;        \
  45.         ++cp;                            \
  46.     }                                \
  47.     return 0;                            \
  48. }
  49.  
  50. /* t_char -> use t_uchar export function */
  51. #define charexp        ucharexp
  52.  
  53. /* t_charv -> use t_ucharv export function */
  54. #define charvexp    ucharvexp
  55.  
  56. /* ucharexp:  t_uchar export function */
  57. static int ucharexp(fp, p, n)
  58. FILE *fp;
  59. const void *p;
  60. size_t n;
  61. {
  62.     unsigned char c = *(unsigned char *)p;
  63.  
  64.     switch (c) {
  65.     case EXPFLDDLM:    /* export file field delimiter */
  66.         if (fputc(EXPESC, fp) == EOF) return -1;
  67.         if (fputc('F', fp) == EOF) return -1;
  68.         break;    /* case EXPFLDDLM: */
  69.     case EXPESC:    /* export file field escape character */
  70.         if (fputc(EXPESC, fp) == EOF) return -1;
  71.         if (fputc(EXPESC, fp) == EOF) return -1;
  72.         break;    /* case EXPESC: */
  73. #if __STDC__ == 1
  74.     case '\a':    /* audible alert */
  75.         if (fputc(EXPESC, fp) == EOF) return -1;
  76.         if (fputc('a', fp) == EOF) return -1;
  77.         break;    /* case '\a': */
  78. #endif
  79.     case '\b':    /* backspace */
  80.         if (fputc(EXPESC, fp) == EOF) return -1;
  81.         if (fputc('b', fp) == EOF) return -1;
  82.         break;    /* case '\b': */
  83.     case '\f':    /* form feed */
  84.         if (fputc(EXPESC, fp) == EOF) return -1;
  85.         if (fputc('f', fp) == EOF) return -1;
  86.         break;    /* case '\f': */
  87.     case '\n':    /* newline */
  88.         if (fputc(EXPESC, fp) == EOF) return -1;
  89.         if (fputc('n', fp) == EOF) return -1;
  90.         break;    /* case '\n': */
  91.     case '\r':    /* carriage return */
  92.         if (fputc(EXPESC, fp) == EOF) return -1;
  93.         if (fputc('r', fp) == EOF) return -1;
  94.         break;    /* case '\r': */
  95.     case '\t':    /* horizontal tab */
  96.         if (fputc(EXPESC, fp) == EOF) return -1;
  97.         if (fputc('t', fp) == EOF) return -1;
  98.         break;    /* case '\t': */
  99.     case '\v':    /* vertical tab */
  100.         if (fputc(EXPESC, fp) == EOF) return -1;
  101.         if (fputc('v', fp) == EOF) return -1;
  102.         break;    /* case '\v': */
  103.     default:
  104.         if (isprint(c)) {
  105.             if (fputc(c, fp) == EOF) return -1;
  106.         } else {
  107.             if (fprintf(fp, "%c%.3o", (int)EXPESC, (int)c) < 0) {
  108.                 return -1;
  109.             }
  110.         }
  111.     }
  112.  
  113.     return 0;
  114. }
  115.  
  116. /* ucharvexp:  t_ucharv export function */
  117. static int ucharvexp(fp, p, n)
  118. FILE *fp;
  119. const void *p;
  120. size_t n;
  121. {
  122.     unsigned char *cp = (unsigned char *)p;
  123.  
  124.     vexp(ucharexp);
  125. }
  126.  
  127. /* shortexp:  t_short export function */
  128. static int shortexp(fp, p, n)
  129. FILE *fp;
  130. const void *p;
  131. size_t n;
  132. {
  133.     signed short x = 0;
  134.  
  135.     memcpy(&x, p, sizeof(x));
  136.     if (fprintf(fp, "%hd", (int)x) < 0) return -1;
  137.  
  138.     return 0;
  139. }
  140.  
  141. /* shortvexp:  t_shortv export function */
  142. static int shortvexp(fp, p, n)
  143. FILE *fp;
  144. const void *p;
  145. size_t n;
  146. {
  147.     signed short *cp = (signed short *)p;
  148.  
  149.     vexp(shortexp);
  150. }
  151.  
  152. /* ushortexp:  t_ushort export function */
  153. static int ushortexp(fp, p, n)
  154. FILE *fp;
  155. const void *p;
  156. size_t n;
  157. {
  158.     unsigned short x = 0;
  159.  
  160.     memcpy(&x, p, sizeof(x));
  161.     if (fprintf(fp, "%hu", (unsigned int)x) < 0) return -1;
  162.  
  163.     return 0;
  164. }
  165.  
  166. /* ushortvexp:  t_ushortv export function */
  167. static int ushortvexp(fp, p, n)
  168. FILE *fp;
  169. const void *p;
  170. size_t n;
  171. {
  172.     unsigned short *cp = (unsigned short *)p;
  173.  
  174.     vexp(ushortexp);
  175. }
  176.  
  177. /* intexp:  t_int export function */
  178. static int intexp(fp, p, n)
  179. FILE *fp;
  180. const void *p;
  181. size_t n;
  182. {
  183.     signed int x = 0;
  184.  
  185.     memcpy(&x, p, sizeof(x));
  186.     if (fprintf(fp, "%d", x) < 0) return -1;
  187.  
  188.     return 0;
  189. }
  190.  
  191. /* intvexp:  t_intv export function */
  192. static int intvexp(fp, p, n)
  193. FILE *fp;
  194. const void *p;
  195. size_t n;
  196. {
  197.     signed int *cp = (signed int *)p;
  198.  
  199.     vexp(intexp);
  200. }
  201.  
  202. /* uintexp:  t_uint export function */
  203. static int uintexp(fp, p, n)
  204. FILE *fp;
  205. const void *p;
  206. size_t n;
  207. {
  208.     unsigned int x = 0;
  209.  
  210.     memcpy(&x, p, sizeof(x));
  211.     if (fprintf(fp, "%u", x) < 0) return -1;
  212.  
  213.     return 0;
  214. }
  215.  
  216. /* uintvexp:  t_uintv export function */
  217. static int uintvexp(fp, p, n)
  218. FILE *fp;
  219. const void *p;
  220. size_t n;
  221. {
  222.     unsigned int *cp = (unsigned int *)p;
  223.  
  224.     vexp(uintexp);
  225. }
  226.  
  227. /* longexp:  t_long export function */
  228. static int longexp(fp, p, n)
  229. FILE *fp;
  230. const void *p;
  231. size_t n;
  232. {
  233.     signed long x = 0;
  234.  
  235.     memcpy(&x, p, sizeof(x));
  236.     if (fprintf(fp, "%ld", x) < 0) return -1;
  237.  
  238.     return 0;
  239. }
  240.  
  241. /* longvexp:  t_longv export function */
  242. static int longvexp(fp, p, n)
  243. FILE *fp;
  244. const void *p;
  245. size_t n;
  246. {
  247.     signed long *cp = (signed long *)p;
  248.  
  249.     vexp(longexp);
  250. }
  251.  
  252. /* ulongexp:  t_ulong export function */
  253. static int ulongexp(fp, p, n)
  254. FILE *fp;
  255. const void *p;
  256. size_t n;
  257. {
  258.     unsigned long x = 0;
  259.  
  260.     memcpy(&x, p, sizeof(x));
  261.     if (fprintf(fp, "%lu", x) < 0) return -1;
  262.  
  263.     return 0;
  264. }
  265.  
  266. /* ulongvexp:  t_ulongv export function */
  267. static int ulongvexp(fp, p, n)
  268. FILE *fp;
  269. const void *p;
  270. size_t n;
  271. {
  272.     unsigned long *cp = (unsigned long *)p;
  273.  
  274.     vexp(ulongexp);
  275. }
  276.  
  277. /* floatexp:  t_float export function */
  278. static int floatexp(fp, p, n)
  279. FILE *fp;
  280. const void *p;
  281. size_t n;
  282. {
  283.     float x = 0;
  284.  
  285.     memcpy(&x, p, sizeof(x));
  286.     if (fprintf(fp, "%G", (double)x) < 0) return -1;
  287.  
  288.     return 0;
  289. }
  290.  
  291. /* floatvexp:  t_floatv export function */
  292. static int floatvexp(fp, p, n)
  293. FILE *fp;
  294. const void *p;
  295. size_t n;
  296. {
  297.     float *cp = (float *)p;
  298.  
  299.     vexp(floatexp);
  300. }
  301.  
  302. /* dblexp:  t_double export function */
  303. static int dblexp(fp, p, n)
  304. FILE *fp;
  305. const void *p;
  306. size_t n;
  307. {
  308.     double x = 0;
  309.  
  310.     memcpy(&x, p, sizeof(x));
  311.     if (fprintf(fp, "%G", x) < 0) return -1;
  312.  
  313.     return 0;
  314. }
  315.  
  316. /* dblexp:  t_double export function */
  317. static int dblvexp(fp, p, n)
  318. FILE *fp;
  319. const void *p;
  320. size_t n;
  321. {
  322.     double *cp = (double *)p;
  323.  
  324.     vexp(dblexp);
  325. }
  326.  
  327. /* ldblexp:  t_ldouble export function */
  328. static int ldblexp(fp, p, n)
  329. FILE *fp;
  330. const void *p;
  331. size_t n;
  332. {
  333. #ifdef AC_LDOUBLE
  334.     long double x = 0;
  335.  
  336.     memcpy(&x, p, sizeof(x));
  337.     if (fprintf(fp, "%lG", x) < 0) return -1;
  338.  
  339.     return 0;
  340. #else
  341.     return 0;
  342. #endif
  343. }
  344.  
  345. /* ldblvexp:  t_ldoublev export function */
  346. static int ldblvexp(fp, p, n)
  347. FILE *fp;
  348. const void *p;
  349. size_t n;
  350. {
  351. #ifdef AC_LDOUBLE
  352.     long double *cp = (long double *)p;
  353.  
  354.     vexp(ldblexp);
  355. #else
  356.     return 0;
  357. #endif
  358. }
  359.  
  360. /* ptrexp:  t_pointer export function */
  361. static int ptrexp(fp, p, n)
  362. FILE *fp;
  363. const void *p;
  364. size_t n;
  365. {
  366.     void *x = NULL;
  367.  
  368.     memcpy(&x, p, sizeof(x));
  369.     if (fprintf(fp, "%p", x) < 0) return -1;
  370.  
  371.     return 0;
  372. }
  373.  
  374. /* ptrvexp:  t_pointerv export function */
  375. static int ptrvexp(fp, p, n)
  376. FILE *fp;
  377. const void *p;
  378. size_t n;
  379. {
  380.     void **cp = (void **)p;
  381.  
  382.     vexp(ptrexp);
  383. }
  384.  
  385. /* strexp:  t_string export function */
  386. static int strexp(fp, p, n)
  387. FILE *fp;
  388. const void *p;
  389. size_t n;
  390. {
  391.     char *s = (char *)p;
  392.     int i = 0;
  393.  
  394.     for (i = 0; i < n; ++i) {
  395.         if (s[i] == NUL) {
  396.             break;
  397.         }
  398.         if (ucharexp(fp, &s[i], sizeof(*s)) == -1) {
  399.             return -1;
  400.         }
  401.     }
  402.  
  403.     return 0;
  404. }
  405.  
  406. /* t_cistring -> use t_string export function */
  407. #define cistrexp    strexp
  408.  
  409. /* binexp:  t_binary export function */
  410. static int binexp(fp, p, n)
  411. FILE *fp;
  412. const void *p;
  413. size_t n;
  414. {
  415.     /* calculate number of hex digits for each char
  416.          # bits in char == CHAR_BIT, # bits in hex digit == 4 */
  417.     const int hexdigits = (CHAR_BIT + (4 - 1)) / 4;
  418.     unsigned char *pi = (unsigned char *)p;
  419.     char fmt[24];            /* printf format string */
  420.  
  421.     sprintf(fmt, "%%.%dX", hexdigits);
  422.     while (pi < ((unsigned char *)p + n)) {
  423.         if (fprintf(fp, fmt, (unsigned int)*pi++) < 0) return -1;
  424.     }
  425.  
  426.     return 0;
  427. }
  428.  
  429. /* export function table definition */
  430. const cbexp_t cbexpv[] = {
  431.     charexp,        /* t_char    =  0 */
  432.     charvexp,        /* t_charv    =  1 */
  433.     ucharexp,        /* t_uchar    =  2 */
  434.     ucharvexp,        /* t_ucharv    =  3 */
  435.     shortexp,        /* t_short    =  4 */
  436.     shortvexp,        /* t_shortv    =  5 */
  437.     ushortexp,        /* t_ushort    =  6 */
  438.     ushortvexp,        /* t_ushortv    =  7 */
  439.     intexp,            /* t_int    =  8 */
  440.     intvexp,        /* t_intv    =  9 */
  441.     uintexp,        /* t_uint    = 10 */
  442.     uintvexp,        /* t_uintv    = 11 */
  443.     longexp,        /* t_long    = 12 */
  444.     longvexp,        /* t_longv    = 13 */
  445.     ulongexp,        /* t_ulong    = 14 */
  446.     ulongvexp,        /* t_ulongv    = 15 */
  447.     floatexp,        /* t_float    = 16 */
  448.     floatvexp,        /* t_floatv    = 17 */
  449.     dblexp,            /* t_double    = 18 */
  450.     dblvexp,        /* t_doublev    = 19 */
  451.     ldblexp,        /* t_ldouble    = 20 */
  452.     ldblvexp,        /* t_ldoublev    = 21 */
  453.     ptrexp,            /* t_pointer    = 22 */
  454.     ptrvexp,        /* t_pointerv    = 23 */
  455.     strexp,            /* t_string    = 24 */
  456.     cistrexp,        /* t_cistring    = 25 */
  457.     binexp            /* t_binary    = 26 */
  458. };
  459.